var DEFAULT_IMG = 'images/tv_default.png';
var navList;
var mIconList;
var curRegion = '';


$(function(){
	
	navList = new NavList({
		id:'navBox',
		onfocus:onfocusNav,
		onclick:onclickNav,
		moveRight:moveRightNav,
		beforeOnclick:beforeOnclickNav,
	});
	mIconList = new IconList({
			id:'iconList',
			disNum:4,
			disRowNum:5,
			itemHeight:120,
			splitHeight:480,
			formatHtml:formatHtml,
			onclick:onclickIcon,
			onfocus:onfocusIcon,
			moveLeft:gotoNav,
			
	});
	
	initData(afterInitData);
	
});

function beforeOnclickNav(type){
	if(type.id==-2){
		if(hasPassPassword){
			return true;
		}else{
			passBox.show();
			return false;
		}
	}else{
		
		return true;
	}
}

function afterInitData(bo){
	hideGPointLoading();
	
	navList.setDataList(typeList,true);
	$('#'+navList.target + '-a-1').click();
}


function formatHtml(i,row,data){
	var img = $.getImageUrl(data.icon,DEFAULT_IMG); 
	var con = '<div class="icon">';
		con += '<img src="'+img+'" />';
		con += '<a href="#" id="'+this.target+'-a-'+i+'" tar="'+i+'" row="'+row+'">&nbsp;</a>';
	con += '</div>';
	return con;
}



function onfocusNav(type,tar){
	curRegion = 'nav';
	$('#infoBox').hide();
}

function onclickNav(type,tar){  
	$('#nav').html(type.name);
	mIconList.setDataList(type.dataList);
}
function moveRightNav(){
	mIconList.setFocus();
}

function onfocusIcon(data,tar){
	if(curRegion!='body'){
		$('#infoBox').show();
	}
	curRegion = 'body';
	
	var img = $.getImageUrl(data.icon,DEFAULT_IMG); 
	$('#tvIcon').html('<img src="'+img+'"/>');
	$('#tvTitle').html(data.displayId+'&nbsp;'+data.name);
	
	
}
function changeProgram(data){
	var con = '';
	var programList = EPGMap['p_'+data.id];
	if(programList){ 

		for(var i = 0;i<programList.length;i++){ 
			if(i>=3)break;
			con += formatProgramHtml(programList[i]);
		}
	}
	if(!con){
		con += '<div class="epgItem"><div class="epgName2">'+lang.live.epgInfo+'.</div></div>';
	}
	
	var epgBox = document.getElementById('epgBox');
	epgBox.innerHTML = con;
}

//格式化节目单
function formatProgramHtml(program,tar){
	var reCat = /\d{1,2}:\d{1,2}/i;
	var beginTime,endTime,name;
	beginTime = reCat.exec(program.beginTime);
	endTime = reCat.exec(program.endTime);
	name = program.name;
	
	var con = '<div class="epgItem"><div class="epgTime">'+beginTime+'-'+endTime+'</div><div class="epgName">'+name+'</div></div>';
	return con;
}


function onclickIcon(data,tar){
	var curTypeId =  navList.curItemId;
	
	
	
	gotoPlayPage(data,curTypeId,tar);
	
}
function gotoNav(){
	navList.setFocus();
}

function gotoPlayPage(data,typeId,itemId){
	$.setSession("typeId",typeId);
	$.setSession("itemId",itemId);
	$.go(config.project+'/channel/play/index.html','play/index.html');
}